using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Services.TfsAlert.Core;
using Microsoft.Services.TfsAlert.Core.Model;

namespace Microsoft.Services.TfsAlert.Forms
{
    /// <summary>
    /// Form to add or edit information about a TFS server including
    /// the name, protocol and port
    /// </summary>
	public partial class AddEditTfsServer: Form
	{
        #region Constants
        
        private const string TITLE_ADD = "Add Team Foundation Server";
        private const string TITLE_EDIT = "Edit Team Foundation Server";

        private const string DEFAULT_SERVER_NAME = "";
        private const string DEFAULT_PORT_NUMBER = "8080";
        private const string DEFAULT_PROTOCOL = "HTTP";

        #endregion

        #region Class Variables

        private bool _inEditMode = false;
        private TfsServer _tfsServer = null;

        #endregion

        #region Ctors

        public AddEditTfsServer()
		{
			InitializeComponent();
        }

        #endregion

        #region Event Handlers

        private void okButton_Click(object sender, EventArgs e)
        {
            portNumberTextbox.Text = portNumberTextbox.Text.Trim();
            serverNameTextbox.Text = serverNameTextbox.Text.Trim();

            if (ValidatePortNumber(portNumberTextbox.Text))
            {
                if (ValidateServer(serverNameTextbox.Text))
                {
                    try
                    {
                        if (_inEditMode == true)
                        {
                            _tfsServer.Name = serverNameTextbox.Text;
                            _tfsServer.URI = CreateURI();
                        }
                        else
                        {
                            // new so create a new server object
                            _tfsServer = new TfsServer();
                            _tfsServer.Name = serverNameTextbox.Text;
                            _tfsServer.URI = CreateURI();
                            // Create a list of events
                            _tfsServer.Events = new List<TfsEvent>();
                        }
                    }
                    catch (Exception)
                    {
                        _tfsServer = null;
                    }

                    Close();
                }
                else
                {
                    MessageBox.Show("The Server must be a valid server name.  Please re-enter");

                    serverNameTextbox.SelectAll();
                }
            }
            else
            {
                MessageBox.Show("The port must be a number 0 < port number < 65535.  Please re-enter");

                portNumberTextbox.SelectAll();
            }

        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            _tfsServer = null;

            Close();
        }

        private void serverNameTextbox_TextChanged(object sender, EventArgs e)
        {
            okButton.Enabled = (serverNameTextbox.Text.Trim().Length > 0);
        }

        #endregion

        #region Support Methods

        /// <summary>
        /// Display as a dialog 
        /// </summary>
        /// <param name="owner">The window to attach this dialog box to</param>
        /// <param name="serverInfo">For Edit mode pass in a TfsServer object and for Add mode pass in null value</param>
        /// <returns>A modified TfsServer object (Edit mode) or a new TfsServer object (Add mode)</returns>
        public TfsServer Show(IWin32Window owner, TfsServer tfsServer)
        {
            if (tfsServer != null)
            {
                _inEditMode = true;
                this.Text = TITLE_EDIT;

                _tfsServer = tfsServer;

                // use the helper object to extract URI information from the TfsServer object
                TfsServerInfoHelper serverInfoHelper = new TfsServerInfoHelper(tfsServer.URI);

                serverNameTextbox.Text = serverInfoHelper.Server;
                portNumberTextbox.Text = serverInfoHelper.Port.ToString();

                SetProtocol(serverInfoHelper.Protocol);
            }
            else
            {
                _tfsServer = null;

                // Set defaults
                serverNameTextbox.Text = DEFAULT_SERVER_NAME;
                portNumberTextbox.Text = DEFAULT_PORT_NUMBER;
                SetProtocol(DEFAULT_PROTOCOL);

                _inEditMode = false;
                this.Text = TITLE_ADD;
            }

            this.ShowDialog(owner);

            return _tfsServer;
        }

        private string CreateURI()
        {
            string port;
            portNumberTextbox.Text = portNumberTextbox.Text.Trim();

            if (string.IsNullOrEmpty(portNumberTextbox.Text))
            {
                port = string.Empty;
            }
            else
            {
                port = string.Format(":{0}", portNumberTextbox.Text);
            }

            string retURI = string.Format("{0}://{1}{2}",
                GetProtocol(), serverNameTextbox.Text, port);

            return retURI;
        }

        private void SetProtocol(string protocol)
        {
            if (protocol.ToUpper() == "HTTP")
            {
                httpRadioButton.Checked = true;
            }
            else
            {
                httpsRadioButton.Checked = true;
            }
        }

        private string GetProtocol()
        {
            string protocol = "http";

            if (httpsRadioButton.Checked)
            {
                protocol = "https";
            }

            return protocol;
        }

        private bool ValidatePortNumber(string port)
        {
            try
            {
                int portNo = int.Parse(portNumberTextbox.Text);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        private bool ValidateServer(string server)
        {
            if (string.IsNullOrEmpty(server.Trim()))
            {
                return false;
            }

            return true;
        }


        #endregion
    }
}